package Syntax;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Item extends Production {
    private int point_index;// 项目中 那个圆点的 位置 point_index = 0 则指向的是 第一个符号 即在list中坐标为0的符号
    private Set<String> forwards = new HashSet<String>();//向前搜索符 规约所依靠的终结符  由First() 产生

    public Item(){}

    public Item(Production new_Prod, int point_index, Set<String> forwards) {
        super(new_Prod);
        this.point_index = point_index;
        this.forwards = forwards;
    }

    public Item(Item new_item)
    {
        super(new_item);
        this.point_index = new_item.getPoint_index();
        this.forwards.addAll( new_item.getForwards() );
    }

    public int getPoint_index() {
        return point_index;
    }

    public void setPoint_index(int point_index) {
        this.point_index = point_index;
    }

    public Set<String> getForwards() {
        return forwards;
    }

    public void setForwards(Set<String> forwards) {
        this.forwards = forwards;
    }

    public void addForward(String forward)
    {
        this.forwards.add(forward);
    }

    public boolean equals(Object obj)
    {
        if( !(obj instanceof  Item))
            return false;
        if( !super.equals(obj))//首先判断 父域内容是否相同
            return false;

        Item i = (Item)obj;
        if( i.getPoint_index()==this.point_index && this.setEquals(i.getForwards(), this.forwards) )
            return true;
        return false;
    }

    public boolean setEquals(Set<String> set1, Set<String> set2)
    {
        if( set1.size() != set2.size() )
            return false;
        for(String s: set1)
        {
            if( !set2.contains( s ))
                return false;
        }
        return true;
    }

    public int hashCode()
    {
        return super.hashCode() + this.point_index*10;
    }
}
